<template>
  <div v-if="keyword !== ''">
    <h1>搜索video</h1>
  </div>
</template>

<script setup>
import {onBeforeMount, onBeforeUnmount, ref, watch} from "vue";
import router from "@/router/index.js";
import Pubsub from "pubsub-js";

const keyword = ref("");

let searchEventKey = Pubsub.subscribe('searchBegin', (msg, data) => {
  if(data !== '') {
    keyword.value = data;
  }
});

watch(keyword, (val) => {
  console.log('搜索video...',val);
});

onBeforeMount(_=> {
  if(typeof router.currentRoute.value.query.keyword !== 'undefined'
      && router.currentRoute.value.query.keyword !== '') {
    keyword.value = router.currentRoute.value.query.keyword;
  }
})

onBeforeUnmount(_=> {
  Pubsub.unsubscribe(searchEventKey);
})
</script>

<style scoped>

</style>